package com.syqc.monitor.ui.historyline

import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isGone
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.LazyFragment
import com.syqc.entity.HistoryLine
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentItineraryBinding
import com.syqc.utils.DataUtil
import com.syqc.utils.Units
import java.text.DecimalFormat

/**
 *@author  hfjs
 *@time   2024/11/20 16:24
 *@desc
 */
class ItineraryFragment :
    LazyFragment<FragmentItineraryBinding>(FragmentItineraryBinding::inflate) {
    private val mAdapter by lazy { ItineraryAdapter() }

    //空布局
    private val emptyView by lazy {
        layoutInflater.inflate(
            R.layout.layout_empty_view,
            binding.rvItinerary,
            false
        )
    }

    override fun initView(view: View) {
        binding.rvItinerary.layoutManager = LinearLayoutManager(requireContext())
        binding.rvItinerary.adapter = mAdapter
        val itineraryData = DataUtil.HistoryLine?.itinerary
        if (itineraryData.isNullOrEmpty()) {
            mAdapter.setEmptyView(emptyView)
            return
        }

        mAdapter.setList(itineraryData)
    }

    override fun initData() {

    }

    override fun initEvent() {

    }
}

class ItineraryAdapter :
    BaseQuickAdapter<HistoryLine.Itinerary, BaseViewHolder>(R.layout.item_itinerary) {
    private val mileDesc by lazy { context.getString(R.string.desc_com_mile) }
    private val avgDesc by lazy { context.getString(R.string.desc_average_speed_) }
    private val maxDesc by lazy { context.getString(R.string.desc_speed_max) }
    override fun convert(
        holder: BaseViewHolder,
        item: HistoryLine.Itinerary
    ) {
        item.run {
            val isStop = maxSpeed == 0
            val llItem = holder.getView<LinearLayout>(R.id.llItem)
            llItem.isSelected = !isStop
            holder.setGone(R.id.llEnd, isStop)
            holder.setVisible(R.id.tvStart, !isStop)
            holder.setText(R.id.tvTitle, desc)
            val tvState = holder.getView<TextView>(R.id.tvState)
            val state =
                "$mileDesc:${getMileDesc(mile)}   $avgDesc:${avgSpeed}${Units.speed}   $maxDesc:${maxSpeed}${Units.speed}"
            tvState.isGone = isStop
            if (isStop) {
                holder.setText(R.id.tvStartTime, "$stime - $etime")
                holder.setText(R.id.tvStartAddr, saddr)
            } else {
                holder.setText(R.id.tvStartTime, stime)
                holder.setText(R.id.tvStartAddr, saddr)
                holder.setText(R.id.tvEndTime, etime)
                holder.setText(R.id.tvEndAddr, eaddr)
                tvState.text = state
            }
        }
    }

    private val df = DecimalFormat("#.00")
    private fun getMileDesc(mile: Int): String {
        return if (mile > 1000) {
            "${df.format(mile / 1000.0)}${Units.km}"
        } else {
            "${mile}${Units.m}"
        }
    }
}